package org.ccams.tran;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ccams.domain.TSalesordersHelp;
import org.ccams.util.PageParameter;

public class Tran15110 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int limit = Integer.parseInt(request.getParameter("limit"));
		int start = Integer.parseInt(request.getParameter("start"));

		String keywords = request.getParameter("keywords");
		String keywords2 = request.getParameter("keywords2");
		keywords = (keywords != null) ? keywords : "";
		keywords2 = (keywords2 != null) ? keywords2 : "";

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new Date().getTime());

		PageParameter page = new PageParameter();
		page.setLimit(limit);
		page.setStart(start);

		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		ArrayList<TSalesordersHelp> al = new ArrayList<TSalesordersHelp>();

		List<TSalesordersHelp> list = null;
		int count = 0;

		if (keywords == "" || keywords.equals("") || keywords2 == ""
				|| keywords2.equals("")) {
			page.setKeyWords(date+" 00:00:00");
			page.setKeywords2(date+" 23:59:59");
			System.out.println("成功进入if语句块.......");
			System.out.println("开始时间" + page.getKeyWords());
			System.out.println("结束时间" + page.getKeywords2());
		} else {
			page.setKeyWords(keywords+" 00:00:00");
			page.setKeywords2(keywords2+" 23:59:59");
			System.out.println("成功进入else语句块.......");
			System.out.println("开始时间" + page.getKeyWords());
			System.out.println("结束时间" + page.getKeywords2());
		}
		list = getCcamsService().getTsalesordershDao().getTSalesordersHByDate(page);
		count = getCcamsService().getTsalesordershDao().getSalesordersCountByDate();


		for (TSalesordersHelp t : list) {
			TSalesordersHelp u = new TSalesordersHelp();
			u.setCompany_name(t.getCompany_name());
			u.setDetp_name(t.getDetp_name());
			u.setOrdername(t.getOrdername());
			u.setNamelist_name(t.getNamelist_name());
			u.setCustomer_name(t.getCustomer_name());
			u.setPesponsible(t.getPesponsible());
			u.setCreated_user(t.getCreated_user());
			u.setLast_update_user(t.getLast_update_user());
			u.setSalesre_person(t.getSalesre_person());
			u.setMeaning(t.getMeaning());
			u.setFlow_status_code(t.getFlow_status_code());
			u.setFlow_status(t.getFlow_status());
			u.setIdt_salesorders_h(t.getIdt_salesorders_h());
			u.setOrder_no(t.getOrder_no());
			u.setOrdered_date(t.getOrdered_date());
			u.setOper_date(t.getOper_date());
			u.setShipping_method_id(t.getShipping_method_id());
			u.setFreight_terms_id(t.getFreight_terms_id());
			u.setCreation_date(t.getCreation_date());
			u.setLast_update_date(t.getLast_update_date());
			u.setCancelled_flag(t.getCancelled_flag());
			u.setOpen_flag(t.getOpen_flag());
			u.setBooked_flag(t.getBooked_flag());
			u.setOrder_money(t.getOrder_money());
			u.setRemark(t.getRemark());
			u.setState(t.getState());

			al.add(u);
		}
		JSONArray JsonArray = JSONArray.fromObject(al); // 得到JSON数组
		jsonMap.put("totalProperty", count);
		jsonMap.put("root", JsonArray);
		json.putAll(jsonMap);

	}
}
